﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MoneyWentWhere.WinFormsUI.UI.DataGrid;
using MoneyWentWhere.DataModel;

namespace MoneyWentWhere.WinFormsUI.UI
{
    public class AutoCompleteForm<T>
        : Form, IAutoCompletePopup<T>
    {
        IAutoCompletePopup<T> _panel;

        public AutoCompleteForm()
        {
            InitializeComponent();

            // Bit hacky, will factor out eventually
            if (typeof(T) == typeof(TxSettings))
            {
                _panel = (IAutoCompletePopup<T>)new MoneyWentWhere.WinFormsUI.UI.DataGrid.TxSettingsPanel();
            }
            AddPanel(_panel as Control);
        }

        void AddPanel(Control panel)
        {
            panel.BackColor = System.Drawing.SystemColors.InactiveCaption;
            panel.Dock = System.Windows.Forms.DockStyle.Fill;
            panel.Name = "panel";
            panel.TabIndex = 0;
            this.Controls.Add(panel);
        }

        void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // AutoCompleteForm
            // 
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
            this.ClientSize = new System.Drawing.Size(236, 284);
            this.ControlBox = false;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "AutoCompleteForm";
            this.ShowInTaskbar = false;
            this.TopMost = true;
            this.ResumeLayout(false);

        }

        #region IAutoCompletePopup Members

        public void ShowPopup(IAutoCompletePopupHost<T> host)
        {
            Show();
            if (_panel != null) { _panel.ShowPopup(host); }
            host.ReturnFocus();
        }

        public void ClosePopup()
        {
            Hide();
            if (_panel != null) { _panel.ClosePopup(); }
        }

        public bool PopupWantsInputKey(Keys key)
        {
            if (Visible == false) { return false; }
            if (_panel != null) { return _panel.PopupWantsInputKey(key); }
            return false;
        }

        public void ProcessInputKey(Keys key)
        {
            if (Visible == false) { return; }
            if (_panel != null) { _panel.ProcessInputKey(key); }
            return;
        }

        public void CommitValue()
        {
            if (_panel != null) { _panel.CommitValue(); }
        }

        #endregion
    }
}
